/*
 * CodeNode.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.apps.codebrowser;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import com.cute4j.swing.Icon;
import com.cute4j.swing.ImageIcon;
import com.cute4j.swing.tree.DefaultMutableTreeNode;

/**
 * Code Node.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class CodeNode extends DefaultMutableTreeNode {

// Private Static Fields. ------------------------------------------------------

  private static final Icon FOLDER_ICON;
  private static final Icon CPP_FILE_ICON;
  private static final Icon H_FILE_ICON;
  private static final Icon JAVA_FILE_ICON;
  private static final Icon JAVA_PACKAGE_ICON;
  private static final Icon UNKNOWN_ICON;

// Static Initialization. ------------------------------------------------------

  static {
    Class codeNodeClass = CodeNode.class;
    FOLDER_ICON = new ImageIcon(codeNodeClass.getResource("Folder.png"));
    CPP_FILE_ICON = new ImageIcon(codeNodeClass.getResource("CppFile.png"));
    H_FILE_ICON = new ImageIcon(codeNodeClass.getResource("HFile.png"));
    JAVA_FILE_ICON = new ImageIcon(codeNodeClass.getResource("JavaFile.png"));
    JAVA_PACKAGE_ICON = new ImageIcon(
                                codeNodeClass.getResource("JavaPackage.png"));
    UNKNOWN_ICON = new ImageIcon(codeNodeClass.getResource("Unknown.png"));
  }

// Private Fields. -------------------------------------------------------------

  private final String title;
  private Icon icon;
  private final File file;
  private boolean expanded; // NOPMD

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new CodeNode object.
   *
   * @param sourceFile the source file
   */
  public CodeNode(File sourceFile) {
    this.file = sourceFile;
    title = file.getName();
  }

  /**
   * Constructs new CodeNode object.
   *
   * @param title the node title
   * @param sourceFile the source file
   */
  public CodeNode(String title, String sourceFile) {
    this.title = title;
    this.file = new File(sourceFile);
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns source file.
   *
   * @return source file
   */
  public File getSourceFile() {
    return file;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public String toString() {
    return title;
  }

  /**
   * {@inheritDoc}
   */
  public int getChildCount() {
    if (!expanded) {
      expanded = true;
      if (file.isDirectory()) {
        String[] rawFiles = file.list();
        Arrays.sort(rawFiles);
        ArrayList folders = new ArrayList(rawFiles.length);
        ArrayList files = new ArrayList(rawFiles.length);
        for (int i = 0; i < rawFiles.length; i++) {
          if (rawFiles[i].charAt(0) != '.' &&
              !rawFiles[i].endsWith(".png")) {
            File currentFile = new File(file, rawFiles[i]);
            if (currentFile.isDirectory()) {
              folders.add(currentFile);
            } else {
              files.add(currentFile);
            }
          }
        }
        rawFiles = null;
        for (int i = 0; i < folders.size(); i++) {
          add(new CodeNode((File)folders.get(i)));
        }
        folders = null;
        for (int i = 0; i < files.size(); i++) {
          add(new CodeNode((File)files.get(i)));
        }
        String absolutePath = file.getAbsolutePath();
        if ((absolutePath.indexOf("java") > 0) &&
            (absolutePath.indexOf("com") > 0)) {
          icon = JAVA_PACKAGE_ICON;
        } else {
          icon = FOLDER_ICON;
        }
      } else {
        if (title.endsWith(".java")) {
          icon = JAVA_FILE_ICON;
        } else if (title.endsWith(".h")) {
          icon = H_FILE_ICON;
        } else if (title.endsWith(".cpp")) {
          icon = CPP_FILE_ICON;
        } else {
          icon = UNKNOWN_ICON;
        }
      }
    }
    return super.getChildCount();
  }

  /**
   * {@inheritDoc}
   */
  public Icon getIcon() {
    return icon;
  }

}
